import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.Set;


public class BookSet< U extends Comparable< ? super U>> implements Set<U> {

	ArrayList<U> store= new ArrayList<U>();
	ArrayList<Integer> count= new ArrayList<Integer>();
	
	BookSet(){
	}

	@Override
	public boolean add(U e) {
		Iterator<U> i = this.iterator();
		int c = 0;
		U cur;
		if(store.contains(e)){
			c=store.indexOf(e);
			int u = count.get(c);
			count.remove(c);
			count.add(c,u+1);
			return true;
		}	
		while(i.hasNext()){
			cur = i.next();
			if (e.compareTo(cur) > 0)
				c++;
		}
		store.add(c,e);
		count.add(c,1);
		return true;
	}

	public boolean addAll(Collection<? extends U> c) {
		Iterator<? extends U> i = c.iterator();
		boolean b = false;
		while (i.hasNext())
			b |= add(i.next());
		return b;
	}

	public void clear() {
		store.clear();
		count.clear();
	}

	@Override
	public boolean contains(Object o) {
		
		return store.contains(o);
	}

	@Override
	public boolean containsAll(Collection<?> c) {
		Iterator<? extends Object> i = c.iterator();
		boolean b = false;
		while(i.hasNext())
			b |= contains(i.next());
		return b;
	}

	@Override
	public boolean isEmpty() {
		return store.isEmpty();
	}

	@Override
	public Iterator<U> iterator() {
		return new Iterator<U>(){
			int i = 0;
			public boolean hasNext() {
				return i < store.size();
			}

			@Override
			public U next() {
				if (hasNext()){
					i++;
					return store.get(i-1);
				}
				return null;
			}

			@Override
			public void remove() {
				throw new UnsupportedOperationException("");
			}
			
		};
	}

	@Override
	public boolean remove(Object o) {
		if(store.contains(o)){
			count.remove(store.indexOf(o));
			store.remove(o);
			return true;
		}
		return false;
	}

	@Override
	public boolean removeAll(Collection<?> c) {
		Iterator<? extends Object> i = c.iterator();
		boolean b = false;
		while(i.hasNext())
			b |= remove(i.next());
		return b;
	}

	@Override
	public boolean retainAll(Collection<?> c) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public int size() {
		int c=0;
		Iterator<Integer> i = count.iterator();
		while(i.hasNext())
			c += i.next();
		return c;
	}

	@Override
	public Object[] toArray() {
		return store.toArray();
	}

	public <U> U[] toArray(U[] a) {
		throw new UnsupportedOperationException() ;

	}


}
